package tpanalisis.main.java;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class MainWindow extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6508764061399916712L;
	private List<Funcion> funciones;
	private Funcion funcionSeleccionada;
	private JPanel contentPane;
	private JFileChooser fileChooser;
	private JButton selectPath;
	@SuppressWarnings("rawtypes")
	private JComboBox comboBox;
	private JButton codigo;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow frame = new MainWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainWindow() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 440, 190);
		setResizable(false);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_contentPane.rowHeights = new int[] { 0, 30, 0, 0, 0 };
		gbl_contentPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		gbl_contentPane.rowWeights = new double[] { 1.0, 0.0, 0.0, 0.0, 0.0 };
		contentPane.setLayout(gbl_contentPane);

		fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		selectPath = new JButton("seleccionar source");
		selectPath.addActionListener(this);
		GridBagConstraints gbc_selectPath = new GridBagConstraints();
		gbc_selectPath.insets = new Insets(0, 0, 5, 5);
		gbc_selectPath.anchor = GridBagConstraints.NORTHWEST;
		gbc_selectPath.gridx = 1;
		gbc_selectPath.gridy = 0;
		contentPane.add(selectPath, gbc_selectPath);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void actionPerformed(ActionEvent action) {
		if (action.getSource() == selectPath) {
			int returnVal = fileChooser.showOpenDialog(MainWindow.this);

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = fileChooser.getSelectedFile();
				if (comboBox == null) {
					comboBox = new JComboBox();
					comboBox.addActionListener(this);
					GridBagConstraints gbc_comboBox = new GridBagConstraints();
					gbc_comboBox.insets = new Insets(0, 0, 5, 0);
					gbc_comboBox.anchor = GridBagConstraints.NORTHWEST;
					gbc_comboBox.gridx = 3;
					gbc_comboBox.gridy = 0;
					contentPane.add(comboBox, gbc_comboBox);
				}
				comboBox.removeAllItems();
				List<String> archivosJava = FileUtils.getInstance().getArchivosFuente(file.getAbsolutePath());
				this.funciones = new ArrayList<Funcion>();
				for (String archivoJava : archivosJava) {
					this.funciones.addAll(FileUtils.getInstance().getFunciones(FileUtils.getInstance().getCodigoDeArchivo(archivoJava)));
					for (Funcion funcion : this.funciones) {
						comboBox.addItem(funcion);
					}
				}
			}
		}
		if (action.getSource() == comboBox) {
			if (action.getActionCommand().equals("comboBoxChanged")) {
				funcionSeleccionada = (Funcion) comboBox.getSelectedItem();
				if (funcionSeleccionada != null) {
					funcionSeleccionada.procesarFuncion(this.funciones);
					this.printLabel("Lineas de codigo: ", funcionSeleccionada.getCantidadLineasDeCodigo().toString(), 2);
					this.printLabel("Porcentaje de comentarios: ", funcionSeleccionada.getPorcentajeComentarios().toString() + "%", 4);
					this.printLabel("fanIn: ", funcionSeleccionada.getFanIn().toString(), 6);
					this.printLabel("fanOut: ", funcionSeleccionada.getFanOut().toString(), 8);
					this.printLabel("complejidad ciclomatica: ", funcionSeleccionada.getComplejidadCiclomatica().toString(), 10);
					if (codigo == null) {
						codigo = new JButton("Ver codigo");
						codigo.addActionListener(this);
						GridBagConstraints gbc_codigo = new GridBagConstraints();
						gbc_codigo.insets = new Insets(0, 0, 5, 0);
						gbc_codigo.anchor = GridBagConstraints.NORTHWEST;
						gbc_codigo.gridx = 1;
						gbc_codigo.gridy = 1;
						contentPane.add(codigo, gbc_codigo);
					}
				}
			}
		}

		if (action.getSource() == codigo) {
			CodeWindow codeWindow = new CodeWindow();
			for (String linea : funcionSeleccionada.getCodigo()) {
				codeWindow.getTextPane().setText(codeWindow.getTextPane().getText() + linea + "\n");
			}
		}
	}

	private void printLabel(String labelText, String result, int numero) {
		JLabel label = null;
		JLabel label_resultado = null;
		try {
			label = (JLabel) contentPane.getComponent(numero);
			label_resultado = (JLabel) contentPane.getComponent(numero + 1);
		} catch (ArrayIndexOutOfBoundsException e) {
		}

		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridy = numero;
		if (label == null) {
			label = new JLabel(labelText);
			gbc.gridx = 1;
			contentPane.add(label, gbc);
		}

		if (label_resultado == null) {
			label_resultado = new JLabel(result);
			gbc.gridx = 2;
			contentPane.add(label_resultado, gbc);
		} else {
			label_resultado.setText(result);
		}
	}
}